package com.atguigu.jxc.service.impl;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.atguigu.jxc.domain.SuccessCode.SUCCESS_CODE;
import static com.atguigu.jxc.domain.SuccessCode.SUCCESS_MESS;

/**
 * @description
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {

	@Autowired
	private LogService logService;

	@Autowired
	private UserDao userDao;

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private ReturnListGoodsDao returnListGoodsDao;

	@Transactional
	@Override
	public ServiceVO save(ReturnList returnList, String returnListGoodsStr) {

		//  PurchaseList 持久化
		User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
		returnList.setUserId(currentUser.getUserId());
		Integer integer = returnListGoodsDao.saveReturnList(returnList);
		if (integer == null || integer == 0) {
			int a=10/0;
			return new ServiceVO(200, "失败");
		}

		//  Goods 持久化
		Gson gson = new Gson();
		List<ReturnListGoods> goodsList = gson.fromJson(returnListGoodsStr, new TypeToken<List<ReturnListGoods>>() {
		}.getType());
		for (ReturnListGoods goods : goodsList) {
			goods.setReturnListId(returnList.getReturnListId());

			//  更新库存……
			Goods byGoodsId = goodsDao.findByGoodsId(goods.getGoodsId());
			byGoodsId.setInventoryQuantity(byGoodsId.getInventoryQuantity()-goods.getGoodsNum());
			goodsDao.updateGoods(byGoodsId);

			Integer integer2 = returnListGoodsDao.saveReturnListGoods(goods);
			if (integer2 == null || integer2 == 0 || byGoodsId.getInventoryQuantity()<0) {
				int a=10/0;
				return new ServiceVO(200, "失败");
			}
		}

		return new ServiceVO(SUCCESS_CODE, SUCCESS_MESS);
	}

	@Override
	public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {

		List<ReturnList> list = returnListGoodsDao.selectReturnList(returnNumber, supplierId, state, sTime, eTime);
		Map<String, Object> map1 = new HashMap<>();
		map1.put("rows", list);
		return map1;
	}


	@Override
	public Map<String, Object> goodsList(Integer returnListId) {
		List<ReturnListGoods> goods = returnListGoodsDao.goodsList(returnListId);
		Map<String, Object> map = new HashMap<>();
		map.put("rows",goods);
		return map;
	}

	@Override
	public ServiceVO delete(Integer returnListId) {
		returnListGoodsDao.delete(returnListId);
		return new ServiceVO(SUCCESS_CODE,SUCCESS_MESS,null);
	}
	@Override
	public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
		List<PurchaseCount> count = returnListGoodsDao.count(sTime, eTime, goodsTypeId, codeOrName);
		return new Gson().toJson(count);
	}

}
